www.gusucode.com > VC snife网络抓包源程序-源码程序 > VC snife网络抓包源程序-源码程序/code/IPPackage/IPPackageDlg.cpp
//Download by http://www.NewXing.com // IPPackageDlg.cpp : implementation file // #include "stdafx.h" #include "IPPackage.h" #include "IPPackageDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif int a,b,c; ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CIPPackageDlg dialog CIPPackageDlg::CIPPackageDlg(CWnd* pParent /*=NULL*/) : CDialog(CIPPackageDlg::IDD, pParent) { //{{AFX_DATA_INIT(CIPPackageDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CIPPackageDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CIPPackageDlg) DDX_Control(pDX, IDC_LIST, m_ctrList); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CIPPackageDlg, CDialog) //{{AFX_MSG_MAP(CIPPackageDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BSTART, OnBstart) ON_BN_CLICKED(IDC_BPAUSE, OnBpause) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList) ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2) ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CIPPackageDlg message handlers BOOL CIPPackageDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //set and initiate the list control m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); m_ctrList.InsertColumn(0, "协议", LVCFMT_CENTER,37); m_ctrList.InsertColumn(1, "源地址", LVCFMT_CENTER,110); m_ctrList.InsertColumn(2, "目的地址", LVCFMT_CENTER,110); m_ctrList.InsertColumn(3, "源端口", LVCFMT_CENTER,60); m_ctrList.InsertColumn(4, "目的端口", LVCFMT_CENTER,60); m_ctrList.InsertColumn(5, "长度", LVCFMT_CENTER,55); InitSock(); // set the Socket to receive all the IP packege //set the state of button GetDlgItem(IDC_BPAUSE)->EnableWindow(false); return TRUE; // return TRUE unless you set the focus to a control } void CIPPackageDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CIPPackageDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CIPPackageDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //the work thread to list the infomation of ip Package UINT WorkThread(LPVOID lpParam) { CIPPackageDlg *dlg = (CIPPackageDlg*)AfxGetApp()->GetMainWnd(); CString str; int nCount; while (TRUE) { //接收原始数据包信息 int ret = recv(dlg->Socket, dlg->RecvBuf, 65535, 0); nCount = dlg->m_ctrList.GetItemCount(); if (ret > 0) { dlg->ip = *(IP*)dlg->RecvBuf; dlg->tcp = *(TCP*)(dlg->RecvBuf + dlg->ip.HdrLen); str.Format("%s",dlg->GetProtocolTxt(dlg->ip.Protocol)); dlg->m_ctrList.InsertItem(0, str); str.Format(" %-15s",inet_ntoa(*(in_addr*)&dlg->ip.SrcAddr)); dlg->m_ctrList.SetItemText(0, 1, str); str.Format(" %-15s",inet_ntoa(*(in_addr*)&dlg->ip.DstAddr)); dlg->m_ctrList.SetItemText(0, 2, str); str.Format(" %-5d",dlg->tcp.SrcPort); dlg->m_ctrList.SetItemText(0, 3, str); str.Format("%-5d",dlg->tcp.DstPort); dlg->m_ctrList.SetItemText(0, 4, str); str.Format(" %-4d",ntohs(dlg->ip.TotalLen)); dlg->m_ctrList.SetItemText(0, 5, str); } } return 1; } void CIPPackageDlg::InitSock() { RecvBuf = new char[65535]; szLocalName = new char [256]; if (WSAStartup(MAKEWORD(1,1), &wsd) != 0) { AfxMessageBox("WSAStartup() failed !"); return ; }//开启winsock.dll //创建raw socket Socket=WSASocket(AF_INET, SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED); char FAR name[128]; gethostname(name, sizeof(name)); struct hostent FAR * pHostent; pHostent = gethostbyname(name); LPSTR lpAddr = pHostent->h_addr_list[0]; struct in_addr inAddr; memmove (&inAddr, lpAddr, 4); CString str = inet_ntoa (inAddr); SetDlgItemText(IDC_STATIC_IP,"本机地址:"+str); SOCKADDR_IN sa; sa.sin_family = AF_INET; sa.sin_port = htons(6000); memcpy(&(sa.sin_addr),pHostent->h_addr,pHostent->h_length); bind(Socket,(LPSOCKADDR)&sa,sizeof(sa));//绑定 // 置WSAIoctl来接收所有网络数据,关键步骤 DWORD dwBufferLen[10] ; DWORD dwBufferInLen = 1 ; DWORD dwBytesReturned = 0 ; WSAIoctl(Socket, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen), &dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL ); } CString CIPPackageDlg::GetProtocolTxt(int Protocol) { switch (Protocol) { case IPPROTO_ICMP : //1 /* control message protocol */ { c++; return PROTOCOL_STRING_ICMP_TXT; } case IPPROTO_TCP : //6 /* tcp */ { b++; return PROTOCOL_STRING_TCP_TXT; } case IPPROTO_UDP : //17 /* user datagram protocol */ {a++; return PROTOCOL_STRING_UDP_TXT; } default: return PROTOCOL_STRING_UNKNOW_TXT; } } void CIPPackageDlg::OnBstart() { // TODO: Add your control notification handler code here GetDlgItem(IDC_BSTART)->EnableWindow(false); GetDlgItem(IDC_BPAUSE)->EnableWindow(true); pThread=AfxBeginThread(&WorkThread,0); } void CIPPackageDlg::OnBpause() { // TODO: Add your control notification handler code here GetDlgItem(IDC_BSTART)->EnableWindow(true); GetDlgItem(IDC_BPAUSE)->EnableWindow(false); TerminateThread(pThread->m_hThread,0); } void CIPPackageDlg::OnButton1() { // TODO: Add your control notification handler code here char* s=new char[100]; char* m=new char[100]; char* n=new char[100]; itoa(a,m,10); itoa(b,s,10); itoa(c,n,10); SetDlgItemText(IDC_EDIT2,m); SetDlgItemText(IDC_EDIT1,s); SetDlgItemText(IDC_EDIT3,n); } void CIPPackageDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here *pResult = 0; } void CIPPackageDlg::OnChangeEdit2() { // TODO: If this is a RICHEDIT control, the control will not int temp = 2; CString s; s.Format( "已上传 %d人", temp ); } void CIPPackageDlg::OnChangeEdit1() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here } void CIPPackageDlg::OnChangeEdit3() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here }